/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucue.ptc.gco.ui;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
//----
import edu.ucue.ptc.gco.mdl.Categoria;
import edu.ucue.ptc.gco.ngc.CategoriaManagerLocal;
import edu.ucue.ptc.gco.util.AtributoInvalidoException;
import edu.ucue.ptc.gco.util.ElementoYaExisteException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import org.primefaces.context.RequestContext;
//----
/**
 *
 * @author Fabian
 */
@Named(value = "categoriaAction")
@SessionScoped
public class CategoriaAction implements Serializable { 
    @Inject 
    private CategoriaManagerLocal categoriaManager;
    private Categoria categoria;

    public boolean isModoModificar() {
        return modoModificar;
    }

    public void setModoModificar(boolean modoModificar) {
        this.modoModificar = modoModificar;
    }
    
    private List<Categoria> listaCategorias;
     private boolean modoModificar = false;
    private int idCategoria;    
    private String nombre;

    public CategoriaManagerLocal getCategoriaManager() {
        return categoriaManager;
    }

    public void setCategoriaManager(CategoriaManagerLocal categoriaManager) {
        this.categoriaManager = categoriaManager;
    }

    public List<Categoria> getListaCategorias() {
        return listaCategorias;
    }

    public void setListaCategorias(List<Categoria> listaCategorias) {
        this.listaCategorias = listaCategorias;
    }

    public int getIdCategoria() {
        return idCategoria;
    }

    public void setIdCategoria(int idCategoria) {
        this.idCategoria = idCategoria;
    }
    
    public Categoria getCategoria() {
        return categoria;
    }

    public void setCategoria(Categoria categoria) {
        this.categoria = categoria;
    }
    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    
    /**
     * Creates a new instance of CategoriaAction
     */
    public CategoriaAction() {
        categoria = new Categoria();
    }
     public static CategoriaAction getInstance()
    {
        ELContext context = FacesContext.getCurrentInstance().getELContext();
        ValueExpression ex = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
                createValueExpression(context, "#{categoriaAction}", CategoriaAction.class);
        return (CategoriaAction)ex.getValue(context);
    }
     @PostConstruct
    public void init() {
        categoria = new Categoria();

        try {
            listaCategorias = categoriaManager.listarTodos(LoginAction.getUsername(), LoginAction.getPassword());
            cargaLista();
        } catch (AtributoInvalidoException ex) {
            Logger.getLogger(ProductoAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ProductoAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     
      public String cancel() {
        return "index";
    }
     public String guardar() {
        try {
          int id = categoria.getIdCategoria();
           
            if (!modoModificar) {
            categoriaManager.crear(categoria, "a", "s");
            } else {
                categoriaManager.actualizar(categoria, LoginAction.getUsername(), LoginAction.getPassword());
                RequestContext.getCurrentInstance().update("tablaP");
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "Guardado"));
                return "listacategoria";
            }
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "Guardado"));
        } catch (AtributoInvalidoException ex) {
            Logger.getLogger(ProductoAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ElementoYaExisteException ex) {
            Logger.getLogger(ProductoAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ProductoAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }
     
      public void cargaLista() {
        try {
            listaCategorias = categoriaManager.listarTodos(LoginAction.getUsername(), LoginAction.getPassword() );
        } catch (Exception ex) {
            Logger.getLogger(ProductoAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  
        public String eliminar(Categoria categoriaMod) {
        try {
            categoriaManager.eliminar(categoriaMod, LoginAction.getUsername(), LoginAction.getPassword());
            listaCategorias.remove(categoriaMod);
            RequestContext.getCurrentInstance().update("tablaP");
        } catch (Exception ex) {
            Logger.getLogger(CategoriaAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "";
    }
     public String modificar(Categoria productoMod) {
        categoria = productoMod;
        modoModificar = true;
        return "categoria";
    }
  
     
}
